﻿using ASPItalia.ModelVirtualCasting.Common;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using Microsoft.Practices.Unity.InterceptionExtension;

namespace ASPItalia.ModelVirtualCasting
{
    internal class UnityRepositoryFactory : IRepositoryFactory
    {
        private IUnityContainer _container;
        private UnityConfigurationSection _configurationSection;

        public UnityRepositoryFactory()
        {
            this.Initialize();
        }

        private void Initialize()
        {
            // Unity container
            _container = new UnityContainer().AddNewExtension<Interception>();

            // Unity configuration
            _configurationSection = Configuration.UnitySection as UnityConfigurationSection;

            // Configure the container
            if (_configurationSection.Containers.Default.IsNotNull())
            {
                _configurationSection.Configure(_container);
            }

            // Setup cache
            _container.AddNewExtension<Interception>().Configure<Interception>().SetInterceptorFor<INewsRepository>(new InterfaceInterceptor());
        }

        public RepositoryFactoryType FactoryType
        {
            get { return RepositoryFactoryType.Unity; }
        }

        public object Container
        {
            get { return _container; }
        }

        public ICandidateRepository CreateCandidateRepository()
        {
            // Resolve the abstract type ICandidateRepository and
            // activate the concrete type specified in the application configuration
            return _container.Resolve<ICandidateRepository>();
        }

        public ICountryRepository CreateCountryRepository()
        {
            // Resolve the abstract type ICountryRepository and
            // activate the concrete type specified in the application configuration
            return _container.Resolve<ICountryRepository>();
        }

        public IJobLevelRepository CreateJobLevelRepository()
        {
            // Resolve the abstract type IJobLevelRepository and
            // activate the concrete type specified in the application configuration
            return _container.Resolve<IJobLevelRepository>();
        }

        public IJobTypeRepository CreateJobTypeRepository()
        {
            // Resolve the abstract type IJobTypeRepository and
            // activate the concrete type specified in the application configuration
            return _container.Resolve<IJobTypeRepository>();
        }

        public ILanguageRepository CreateLanguageRepository()
        {
            // Resolve the abstract type ILanguageRepository and
            // activate the concrete type specified in the application configuration
            return _container.Resolve<ILanguageRepository>();
        }

        public IModelRepository CreateModelRepository()
        {
            // Resolve the abstract type IModelRepository and
            // activate the concrete type specified in the application configuration
            return _container.Resolve<IModelRepository>();
        }

        public INewsRepository CreateNewsRepository()
        {
            // Resolve the abstract type INewsRepository and
            // activate the concrete type specified in the application configuration
            return _container.Resolve<INewsRepository>();
        }

        public IPersonRepository CreatePersonRepository()
        {
            // Resolve the abstract type IPersonRepository and
            // activate the concrete type specified in the application configuration
            return _container.Resolve<IPersonRepository>();
        }

        public IRequestRepository CreateRequestRepository()
        {
            // Resolve the abstract type IRequestRepository and
            // activate the concrete type specified in the application configuration
            return _container.Resolve<IRequestRepository>();
        }

        public IUserRepository CreateUserRepository()
        {
            // Resolve the abstract type IUserRepository and
            // activate the concrete type specified in the application configuration
            return _container.Resolve<IUserRepository>();
        }

        public IUserRoleRepository CreateUserRoleRepository()
        {
            // Resolve the abstract type IUserRoleRepository and
            // activate the concrete type specified in the application configuration
            return _container.Resolve<IUserRoleRepository>();
        }
    }
}
